package com.verisette.ncos.service.card.bizunit;

import java.sql.Timestamp;

import org.apache.log4j.Logger;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;

import com.verisette.ncos.persistence.dao.MemberDAO;
import com.verisette.ncos.persistence.domain.MasterCompany;
import com.verisette.ncos.persistence.domain.MasterMember;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.web.card.bean.CardCreateBean;

public class InsertMemberBU extends CommonBusinessUnit {
	private Logger logger = Logger.getLogger(InsertMemberBU.class);
	
	MemberDAO memberDAO;
	@Required
	@Autowired
	public void setMemberDAO(MemberDAO memberDAO) {
		this.memberDAO = memberDAO;
	}	
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute" );
		
		MasterUserProfile userBean = (MasterUserProfile) processContext.getUserBean();
		MasterCompany company = userBean.getMasterCompany();
		
		MasterMember member = null;
		Object o = processContext.getOriginalRequest().getValueObject();
		if(o instanceof CardCreateBean){
			member = ( (CardCreateBean) o).getOrder().getMasterMember(); 
		} else if(o instanceof MasterMember){
			member = (MasterMember) o;
		}
		
		if(company!=null){
			member.setMmbCompanyNameTh(company.getCompanyNameTh());
			member.setMmbCompanyNameEn(company.getCompanyNameEn());
		}
//		member.setMmbEmail(userBean.getUspEmail());
		member.setMmbCreatedBy(userBean.getUsername());
		member.setMmbCreatedDate(new Timestamp(System.currentTimeMillis()));
		
		member.setMasterBranch(userBean.getMasterBranch());
		member.setMasterCompany(userBean.getMasterCompany());
		memberDAO.save(member);
		
		processContext.setValueObject(o);
	}


}